<?php

namespace App\Http\Controllers\Cruise\Web;

use App\Http\Controllers\BaseController;
use App\Models\Common\ResourcesModel;
use App\Models\Cruise\CruiseOrderModel;
use App\Models\Cruise\CruiseTemplateModel;
use App\Service\Cruise\Check\CruiseCheckDetailService;
use App\Service\Cruise\Check\CruiseCheckTaskService;
use App\Service\Cruise\CruiseTour\CruiseTourService;
use App\Service\Cruise\Mission\CruiseTourInputService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;

class CruiseTourController extends BaseController
{


    /**
     * 巡检记录列表
     * @param Request $request
     * @return JsonResponse
     */
    public function list(Request $request): JsonResponse
    {
        $this->validate(
            $request, [
                'shopId' => 'required|integer',
            ]
        );
        $pageSize = (int)$request->get('pageSize', 20);
        $pageNum  = (int)$request->get('pageNum', 1);
        $deviceId = (int)$request->get('deviceId', 0);
        $shopId   = (int)$request->get('shopId', 0);

        return $this->success(CruiseTourService::getList($shopId, $pageSize, $pageNum));
    }


    /**
     * 巡检记录详情
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function info(Request $request, int $id): JsonResponse
    {
        return $this->success(CruiseTourService::info($id));
    }

    /**
     * 巡检任务新增记录
     * @param Request $request
     * @return JsonResponse
     */
    public function inputsAdd(Request $request): JsonResponse
    {
        $this->validate(
            $request, [
                'shopId' => 'required|integer',
            ]
        );
        $shopId = (int)$request->get('shopId', 0);
        $userId = getRmsUserId();

        $orderModel = CruiseOrderModel::query()
            ->where('shopId', $shopId)
            ->where('type', CruiseOrderModel::TYPE_CHECK_TOUR)
            ->where('handlerId', $userId)
            ->first();
        if (!$orderModel) {
            CruiseTourService::initOrder($shopId, $userId);
        }

        return $this->success();
    }

    /**
     * 巡检任务记录详情
     * @param Request $request
     * @return JsonResponse
     */
    public function inputsInfo(Request $request): JsonResponse
    {
        $this->validate(
            $request, [
                'shopId' => 'required|integer',
            ]
        );
        $shopId = (int)$request->get('shopId', 0);
        $userId = getRmsUserId();

        $cruiseOrderModel = (new CruiseTourService)->getOrder($shopId, $userId, true);
        return $this->success((new CruiseTourInputService($cruiseOrderModel->id, $shopId, $userId))->getInputObjectFill());
    }


    /**
     * 巡检任务记录修改
     * @param Request $request
     * @return JsonResponse
     */
    public function inputsUpdate(Request $request): JsonResponse
    {
        $this->validate(
            $request, [
            'shopId'     => 'required|integer',
            'field'      => 'required|string',
            'value'      => 'required',
            'auditValue' => 'array',
        ]
        );

        $field      = (string)$request->get('field');
        $value      = $request->get('value');
        $auditValue = (array)$request->get('auditValue');
        $shopId     = (int)$request->get('shopId', 0);
        $userId     = getRmsUserId();

        $cruiseOrderModel = (new CruiseTourService)->getOrder($shopId, $userId);
        (new CruiseTourInputService($cruiseOrderModel->id, $shopId, $userId))->saveUserContentJson($field, $value,$auditValue);

        return $this->success();
    }

    /**
     * 巡检任务记录提交
     * @param Request $request
     * @return JsonResponse
     */
    public function inputsSubmit(Request $request): JsonResponse
    {
        $this->validate(
            $request, [
                'shopId' => 'required|integer',
            ]
        );
        $shopId = (int)$request->get('shopId', 0);
        $userId = getRmsUserId();

        $cruiseOrderModel = (new CruiseTourService)->getOrder($shopId, $userId);
        (new CruiseTourInputService($cruiseOrderModel->id, $shopId, $userId))->submitOrder();

        return $this->success();
    }

    /**
     * 截图、视频资源库列表
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function resourcesList(Request $request): JsonResponse
    {
        $this->validate(
            $request, [
                'shopId' => 'required|integer',
            ]
        );
        $pageSize  = (int)$request->get('pageSize', 20);
        $pageNum   = (int)$request->get('pageNum', 1);
        $deviceId  = (int)$request->get('deviceId', 0);
        $shopId    = (int)$request->get('shopId', 0);
        $type      = (string)$request->get('type', '');
        $startTime = (string)$request->get('startTime', '');
        $endTime   = (string)$request->get('endTime', '');
        return $this->success((new CruiseTourService)->resourcesList($shopId, $type, $deviceId, $startTime, $endTime, $pageSize, $pageNum));
    }

    /**
     * 截图、视频资源上传
     * @param Request $request
     * @return JsonResponse
     */
    public function uploadResources(Request $request): JsonResponse
    {
        $files    = (array)$request->post('files');
        $deviceId = (int)$request->post('deviceId', 0);
        $shopId   = (int)$request->post('shopId');

        $this->validate(
            $request, [
                'files'             => 'required|array',
                'files.*.url'       => 'required|string',
                'files.*.type'      => ['required', 'string', Rule::in(ResourcesModel::EXTRA_TYPES)],
                'files.*.name'      => 'required|string',
                'files.*.thumbnail' => 'string',
                'deviceId'          => 'integer',
                'shopId'            => 'required|integer',
            ]
        );
        $userId = getRmsUserId();
        (new CruiseTourService)->uploadResources($files, $shopId, $userId, $deviceId);
        return $this->success();
    }


    /**
     * 截图、视频资源库详情
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ValidationException
     */
    public function getResources(Request $request, int $id): JsonResponse
    {
        $this->validate(
            $request, [
                'shopId' => 'required|integer',
            ]
        );
        $deviceId = (int)$request->get('deviceId', 0);
        $shopId   = (int)$request->get('shopId', 0);
        return $this->success((new CruiseTourService)->getResources($shopId, $id));
    }

    /**
     * 截图、视频资源库删除
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function delResources(Request $request, int $id): JsonResponse
    {
        $this->validate(
            $request, [
                'deviceId' => 'integer',
                'shopId'   => 'required|integer',
            ]
        );
        $userId = getRmsUserId();
        $shopId = (int)$request->get('shopId', 0);
        (new CruiseTourService)->delResources($shopId, $id, $userId);
        return $this->success();
    }


    /**
     * 作废任务
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function inputsCancelOrder(Request $request, int $id)
    {
        $userId = getRmsUserId();
        $invalidType = (int)$request->post('invalidType', 0);
        $video       = $request->post('video', []);
        $explain     = (string)$request->post('explain', '');
        CruiseCheckTaskService::orderCancel($id, $userId, true, $invalidType, $explain, $video);

        return $this->success();
    }
}
